﻿using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;
using System.Text;

namespace Xppend.CodeGenerator.Decorators
{
    internal class AppendCostTimeDecorator : BaseDecorator<MethodDeclarationSyntax>
    {
        private const string AttributeName = "AppendCostTime";

        public override bool HasDecorator(MethodDeclarationSyntax dataContext)
        {
            var hasAttribute = dataContext.AttributeLists.Any(a => a.Attributes.Any(sa => sa.Name.ToString().Contains(AttributeName)));

            return hasAttribute;
        }

        public override void Decorate(StringBuilder stringBuilder, MethodDeclarationSyntax dataContext)
        {
            stringBuilder.AppendLine("var xStartTime = DateTime.Now;");
            stringBuilder.AppendLine($"var xMethodName = \"{dataContext.Identifier.Text}\";");

            stringBuilder.AppendLine(dataContext.Body.ToString());

            stringBuilder.AppendLine("var xCost = DateTime.Now - xStartTime;");
            stringBuilder.AppendLine("Console.WriteLine($\"{xMethodName} timecost {xCost}\");");
        }

        public static AppendCostTimeDecorator Instance { get; private set; } = new AppendCostTimeDecorator();
        //public AppendCostTimeDecorator()
        //{
        //    var xStartTime = DateTime.Now;
        //    var xMethodName = "xxx";

        //    var xCost = DateTime.Now - xStartTime;
        //    Console.WriteLine($"{xMethodName} 计时花费 {xCost}");

        //}
    }
}
